package com.jeespring.rocketmq.filter.tag;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

/**
 * @description Consumer
 * @author 张朝阳
 * @date 2021/1/10 22:07
 */
public class Consumer {
    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("group1");

        consumer.setNamesrvAddr("192.168.19.10:9876");
        // 消费模式默认负载均衡，MessageModel.BROADCASTING：广播模式
        consumer.setMessageModel(MessageModel.BROADCASTING);

        // Subscribe one more more topics to consume.
//        consumer.subscribe("filter-message", "*");
        consumer.subscribe("filter-message", "tag1 || tag2");
        // Register callback to execute on arrival of messages fetched from brokers.
        consumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
            for (MessageExt msg : msgs) {
                byte[] body = msg.getBody();
                System.out.printf("%s 接收消息: %s %n", Thread.currentThread().getName(), new String(body));
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });


        //Launch the consumer instance.
        consumer.start();
    }
}
